//
//  ProgressCellView.m
//  TcpTest_Client
//
//  Created by scarlettsun on 12-8-1.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "HiQProgressCellView.h"
#import "MainWindowController.h"
#import "FileWindowController.h"

@implementation HiQProgressCellView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [sendProgress setIndeterminate:NO];
        tfStatus.stringValue = @"preparing";
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
    [super drawRect:dirtyRect];
}

- (HiQProgressCellView *) initWithFileEntity:(FileEntity *)_file
{
    [photo setImage: [NSImage imageNamed:_file.photo]];
    [sendProgress setMaxValue:_file.fileSize];
    [sendProgress setMinValue:0.0f];
    NSString *fileInfo = [[NSString alloc] initWithFormat:@"%@  %@", [_file.fileUrl lastPathComponent] ,[self transfileInfo:_file.fileSize]];
    tfFileInfo.stringValue = fileInfo;
    NSString *statusInfo = @"";
    if (_file.receiverId != ((MainWindowController*)[MainWindowController sharedMainWindowController]).myselfInfo.buddyQQ )  //send file
    {
        [sendProgress setDoubleValue:_file.fileSentSize];
        [reviewButton setImage:[NSImage imageNamed: NSImageNameRevealFreestandingTemplate]];
        [reviewButton setToolTip:NSLocalizedString(@"浏览文件", nil)];
        [reviewButton setBordered:YES];
        tfSender.stringValue = [NSString stringWithFormat:@"%lu",_file.receiverId];
        switch (_file.fileState) 
        {
            case EntityWaiting:
            {
                statusInfo = NSLocalizedString(@"等待对方接收", nil); 
                break;
            }   
            case EntityReceive:
            {
                statusInfo = [[NSString alloc] initWithFormat:NSLocalizedString(@"已发送%@/%@", nil),[self transfileInfo:_file.fileSentSize],[self transfileInfo:_file.fileSize]];
                break;
            }
            case EntityFinish:
            {
                statusInfo = NSLocalizedString(@"发送成功", nil); 
                [removeButton setHidden:YES];
                //[reviewButton setImage:[NSImage imageNamed:@"comment_add.png"]];
                break;
            }
            case EntityStop:
            {
                [removeButton setHidden:YES];
                statusInfo = [[NSString alloc] initWithFormat:NSLocalizedString(@"传输中止,已发送%@/%@", nil),[self transfileInfo:_file.fileSentSize],[self transfileInfo:_file.fileSize]]; 
                break;
            }
            case EntityReject:
            {
                [removeButton setHidden:YES];
                statusInfo = NSLocalizedString(@"对方已拒绝接收文件", nil); 
                break;
            }
            case EntityCancel:
            {
                statusInfo = NSLocalizedString(@"您已取消文件发送", nil); 
                [removeButton setHidden:YES];
                break;
            }
            default:
                break;
        }
    } 
    else 
    { 
        [sendProgress setDoubleValue:_file.fileReceiveSize];
         tfSender.stringValue = [NSString stringWithFormat:@"%lu",_file.senderId];
        switch (_file.fileState) 
        {
            case EntityWaiting:
            {
                statusInfo = NSLocalizedString(@"准备接收", nil); 
                [reviewButton setImage:[NSImage imageNamed:@"down.png"]];
                [reviewButton setBordered:NO];
                [reviewButton setToolTip:NSLocalizedString(@"接收文件", nil)];
                break;
            }   
            case EntityReceive:
            {
                [reviewButton setImage:[NSImage imageNamed: NSImageNameRevealFreestandingTemplate]];
                [reviewButton setToolTip:NSLocalizedString(@"浏览文件", nil)];
                [reviewButton setBordered:YES];
                statusInfo = [[NSString alloc] initWithFormat:NSLocalizedString(@"已接收%@/%@", nil),[self transfileInfo:_file.fileReceiveSize],[self transfileInfo:_file.fileSize]];  
                break;
            }
            case EntityFinish:
            {
                statusInfo = NSLocalizedString(@"接收成功", nil);
                [removeButton setHidden:YES];
                [reviewButton setImage:[NSImage imageNamed: NSImageNameRevealFreestandingTemplate]];
                [reviewButton setToolTip:NSLocalizedString(@"浏览文件", nil)];
                [reviewButton setBordered:YES];

                break;
            }
            case EntityStop:
            {
                [removeButton setHidden:YES];
                [reviewButton setImage:[NSImage imageNamed: NSImageNameRevealFreestandingTemplate]];
                [reviewButton setToolTip:NSLocalizedString(@"浏览文件", nil)];
                [reviewButton setBordered:YES];
                statusInfo = [[NSString alloc] initWithFormat:NSLocalizedString(@"传输中止,已接收%@/%@", nil),[self transfileInfo:_file.fileReceiveSize],[self transfileInfo:_file.fileSize]];  
                break;
            }
            case EntityCancel:
            {
                statusInfo = NSLocalizedString(@"对方已取消文件发送", nil);
                [removeButton setHidden:YES];
                [reviewButton setHidden:YES];
                break;
            }
            case EntityReject:
            {
                [removeButton setHidden:YES];
                [reviewButton setHidden:YES];
                statusInfo = NSLocalizedString(@"您已拒绝接收此文件", nil); 
                break;
            }
            default:
                break;
        }
    }
    tfStatus.stringValue = statusInfo;
    return self;
}

- (NSString *) transfileInfo:(NSUInteger)_transsize
{
    NSString *info;
    if (_transsize >1000000) 
    {
        info = [[NSString alloc] initWithFormat:@"%0.1fMB",((double)_transsize/1000/1000)];
    }
    else if(_transsize > 1000)
    {
        info = [[NSString alloc] initWithFormat:@"%0.1fKB",((double)_transsize/1000)];
    }
    else 
    {
        info = [[NSString alloc] initWithFormat:@"%0.2fB",((double)_transsize)];
    }
    return  info;
}

@end
